<template>
  <div class="pad-content-box">
    <form-common class="margin-top-20" :filed-list="summaryInfoFiledList" ref="form"></form-common>
    <div class="flex margin-top-20">
      <span class="font-size-16 color-333333 padding-top-5">会议纪要</span>
      <upload-file class="upload-box" :disabled="meetObj.meetingStatus == 1 ? false : true" buttonType="text" btnName="上传附件" :is-multiple="false" filed-name="file" :max-size="100" accept-file-type=".xlsx,.xls,.csv,.png,.jpg,.Png,.Jpg,.pdf" :file-list="fileList" file-url-name="fileUrl" file-txt-name="fileTitle" @handleSuccess="handleSuccess"> </upload-file>
    </div>

    <template v-if="$route.query.meetingKey">
      <div class="font-size-16 font-weight-700 margin-top-bottom-20">上一次会议动作</div>
      <work-table type="last" ref="lastWork" :is-handle="true" @refresh="getInfo" :meeting-status="meetObj.meetingStatus" :list="lastWorkList"></work-table>

      <div class="flex align-centers margin-top-bottom-10">
        <span class="font-size-16 font-weight-700">会议动作</span>
        <el-button type="text" class="margin-left-30" @click="addAction" v-if="meetObj.meetingStatus == 1">添加</el-button>
      </div>
      <work-table type="now" ref="nowWork" :meeting-status="meetObj.meetingStatus" :list="nowWorkList" @seaFn="getItemInfo"></work-table>
    </template>

    <add-work ref="work" @refresh="getInfo" :btn-show="btnShow"></add-work>

    <div v-if="meetObj.meetingStatus == 1" class="margin-top-15">
      <el-button type="primary" size="mini" @click="saveFn" class="f-right">{{$route.query.meetingKey ? '保存' : '创建会议动作'}}</el-button>
      <el-button type="primary" size="mini" @click="saveFn('publish')" class="margin-right-10 f-right">发布</el-button>
    </div>
  </div>
</template>
<script>
// meetingStatus : 1是保存 2是提交
import merge from 'webpack-merge';
import formCommon from '@g/components/form/formCommon.vue';
import { summaryInfoFiledList } from './form';
import uploadFile from '../../components/upload.vue';
import workTable from './components/workTable';
import addWork from './components/addWork';
import { apiMeetingAdd, apiMeetingInfo, apiMeetingUpdate } from '../../api/workTrace';
import { requestTips } from '../../assets/util';

export default {
  data() {
    return {
      summaryInfoFiledList: summaryInfoFiledList,
      fileList: [],
      nowWorkList: [],
      lastWorkList: [],
      meetObj: {meetingStatus:1},
      btnShow:true
    };
  },

  mounted() {
    if (this.$route.query.meetingKey) {
      this.getInfo();
    }
  },

  methods: {
    //添加动作
    addAction() {
      this.btnShow = true
      this.$refs.work.init();
    },

    //编辑动作
    getItemInfo(item) {
      this.btnShow = item.status == 1 ? false : true
      this.$refs.work.callBackFn(item);
    },

    //保存
    async saveFn(type) {
      let model = this.$refs.form.validateForm();
      if (this.fileList.length == 0) {
        this.$message.warning('请上传会议纪要附件');
        return;
      }
      this.fileList.forEach(item => {
        item.primaryKey = '';
      });
      model.attachmentList = this.fileList;
      model.meetingStatus = type == 'publish' ? 2 : 1;
      if (this.meetObj.primaryKey) {
        model.primaryKey = this.meetObj.primaryKey;
      }
      console.log(model);
      let res = this.meetObj.primaryKey ? await apiMeetingUpdate(model) : await apiMeetingAdd(model);

      requestTips(res, async () => {
        if (this.meetObj.primaryKey) {
          this.getInfo();
        } else {
          this.$router.replace({
            query: merge(this.$route.query, { meetingKey: res.key }),
          });
          this.getInfo();
        }
      });
    },

    //详情
    async getInfo() {
      summaryInfoFiledList[0].bindFieldVal = '';
      let res = await apiMeetingInfo({ primaryKey: this.$route.query.meetingKey });
      this.meetObj = res;
      this.$refs.form.initModel();
      this.setFiledVal(summaryInfoFiledList, res);
      this.fileList = res.attachmentList || [];
      this.nowWorkList = res.currentMeetingActionList || [];
      this.lastWorkList = res.lastMeetingActionList || [];
    },

    //回显
    setFiledVal(list, row) {
      list.forEach(item => {
        for (let i in row) {
          if (i == item.bindFieldName) {
            item.bindFieldVal = row[i];
          }
        }
      });
    },

    //上传成功
    handleSuccess(file, fileList) {
      if (file.data.rows && file.data.rows.length > 0) {
        this.fileList.push({
          fileId: file.data.rows[0].key,
          fileUrl: file.data.rows[0].url,
          fileTitle: file.data.rows[0].originalName,
        });
      } else {
        this.fileList = [];
        this.$message.error(file.data.message);
      }
    },
  },

  components: { formCommon, uploadFile, workTable, addWork },
};
</script>
<style lang="scss" scoped>
::v-deep .el-form .el-form-item__label {
  padding-left: 0;
}
.upload-box {
  margin-left: 44px;
}
</style>
